package com.terren.webdig.setting.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.terren.webdig.basic.BasicControllerSupport;
import com.terren.webdig.common.util.CommonUtil;
import com.terren.webdig.common.util.JsonUtil;
import com.terren.webdig.setting.entity.Role;
import com.terren.webdig.setting.entity.RoleFunction;
import com.terren.webdig.setting.service.IRoleService;

@Controller
@RequestMapping(value = "/role")
public class RoleController extends BasicControllerSupport {

	@Resource(name = "roleService")
	private IRoleService roleService;

	/**
	 * 获取角色信息
	 * 
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getRoleList", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getRoleList(HttpServletRequest request) {
		String result = "";
		Role role = (Role) CommonUtil.getParameterObject(Role.class, request);
		try {
			List<Role> roleList = roleService.getRoleList(role);
			result = JsonUtil.pagingJson(roleList);
		} catch (Exception e) {
			log.error("RoleController.getRoleList error", e);
		}
		return result;
	}

	/**
	 * 根据id获取角色信息
	 * 
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getRoleByIdAll", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getRoleByIdAll(HttpServletRequest request) {
		String result = "";
		Role role = (Role) CommonUtil.getParameterObject(Role.class, request);
		try {
			Role roleTemp = roleService.getRoleByIdAll(role);
			result = JsonUtil.toJSONString(roleTemp);
		} catch (Exception e) {
			log.error("RoleController.getRoleById error", e);
		}
		return result;
	}

	/**
	 * 根据id获取角色信息
	 * 
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/isRepeat", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String isRepeat(HttpServletRequest request) {
		String result = "";
		Role role = (Role) CommonUtil.getParameterObject(Role.class, request);
		try {
			List<Role> roleList = roleService.isRepeat(role);
			result = "0";// 代表无重复的用户名
			if (roleList.size() > 0) {
				result = "1";
			}
		} catch (Exception e) {
			log.error("RoleController.isRepeat", e);
		}
		return result;
	}

	/**
	 * 增加角色及功能
	 * 
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveRole", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String saveRole(@RequestParam("roleName") String roleName, @RequestParam("functionIds") String allIds) {
		String result = "";
		Set<String> functionsIdsSet = new HashSet<String>();
		String[] allIdArr = allIds.split(",");
		for (String s : allIdArr) {
			String[] functionIdArray = s.split("_");
			functionsIdsSet.add(functionIdArray[0]);
			functionsIdsSet.add(functionIdArray[1]);
		}
		try {
			String functionIds = "";
			for (String functionId : functionsIdsSet) {
				functionIds = functionId + "," + functionIds;
			}
			Role role = new Role();
			role.setRoleName(roleName);
			role.setFunctionIds(functionIds);
			roleService.saveRole(role);
			result = JsonUtil.messageJson(true, "添加成功");
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "添加失败");
			log.error("RoleController.saveRole error", e);
		}
		return result;
	}

	/**
	 * 修改用户
	 * 
	 * @param roleId
	 * @param roleName
	 * @param allIds
	 * @return
	 */
	@RequestMapping(value = "/updateRole", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateRole(@RequestParam("roleId") String roleId, @RequestParam("roleName") String roleName,
			@RequestParam("functionIds") String allIds) {
		String result = "";
		Set<String> functionsIdsSet = new HashSet<String>();
		log.info("========allIds======" + allIds);
		String[] allIdArr = allIds.split(",");
		for (String s : allIdArr) {
			String[] functionIdArray = s.split("_");
			functionsIdsSet.add(functionIdArray[0]);
			functionsIdsSet.add(functionIdArray[1]);
		}
		try {
			String functionIds = "";
			for (String functionId : functionsIdsSet) {
				functionIds = functionId + "," + functionIds;
			}
			Role role = new Role();
			role.setRoleName(roleName);
			role.setRoleId(Integer.parseInt(roleId));
			role.setFunctionIds(functionIds);
			String returnData = roleService.updateRole(role);
			log.info(returnData);
			result = JsonUtil.messageJson(true, "修改成功");
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "修改失败");
			log.error("RoleController.updateUser error", e);
		}
		return result;
	}

	/**
	 * 角色及功能删除
	 */
	@RequestMapping(value = "/deleteRole", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String deleteRole(HttpServletRequest request) {
		String result = "";
		Role param = (Role) CommonUtil.getParameterObject(Role.class, request);
		RoleFunction fun = new RoleFunction();
		try {
			fun.setRoleId(param.getRoleId());
			roleService.deleteRole(param);
			roleService.deleteAllRoleFunction(fun);
			result = JsonUtil.messageJson(true, "删除成功");
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "删除失败");
			log.error("RoleController.deleteRole error", e);
		}
		return result;
	}
}
